package br.com.tcf.Conexao;

import java.io.IOException;
import java.util.Properties;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

import br.com.tcf.entidades.Acrescimo;
import br.com.tcf.entidades.Cardapio;
import br.com.tcf.entidades.Cidade;
import br.com.tcf.entidades.Cliente;
import br.com.tcf.entidades.DiaSemana;
import br.com.tcf.entidades.Empresa;
import br.com.tcf.entidades.Estado;
import br.com.tcf.entidades.Produto;
import br.com.tcf.entidades.ProdutoDetalhe;
import br.com.tcf.entidades.ProdutoDetalhePromocao;
import br.com.tcf.entidades.Promocao;
import br.com.tcf.entidades.TpAlimento;
import br.com.tcf.entidades.Vigencia;
import br.com.tcf.entidades.VigenciaAcrescimo;
import br.com.tcf.login.Grupo;
import br.com.tcf.login.Permissao;
import br.com.tcf.login.Usuario;

public class Connection{
	private static AnnotationConfiguration configuration;
	private static Properties properties = null;
	private static SessionFactory factory;

	private SessionFactory factoryTeste;

	public Connection(){
		if(properties == null){
			System.out.println("Pegando o properties");
			properties = returnArquivoConfiguracao(caminhoMysql(), "config_bd.properties");
		}

		configuration = new AnnotationConfiguration(); 
		configuration.setProperty("hibernate.current_session_context_class", "thread");
		configuration.setProperty("hibernate.dialect", properties.getProperty("dialect"));
		configuration.setProperty("hibernate.connection.driver_class", properties.getProperty("driver_class"));
		configuration.setProperty("hibernate.connection.url", properties.getProperty("url"));
		configuration.setProperty("hibernate.connection.username", properties.getProperty("username"));
		configuration.setProperty("hibernate.connection.password", properties.getProperty("password"));
		configuration.setProperty("hibernate.show_sql", properties.getProperty("show_sql"));
		configuration.setProperty("hibernate.hbm2ddl.auto", properties.getProperty("hbm2ddl"));

		configuration.addAnnotatedClass(Usuario.class);
		configuration.addAnnotatedClass(Grupo.class);
		configuration.addAnnotatedClass(Permissao.class);
		configuration.addAnnotatedClass(Cardapio.class);
		configuration.addAnnotatedClass(Cidade.class);
		configuration.addAnnotatedClass(Empresa.class);
		configuration.addAnnotatedClass(Estado.class);
		configuration.addAnnotatedClass(Produto.class);
		configuration.addAnnotatedClass(Acrescimo.class);
		configuration.addAnnotatedClass(TpAlimento.class);
		configuration.addAnnotatedClass(Vigencia.class);
		configuration.addAnnotatedClass(VigenciaAcrescimo.class);
		configuration.addAnnotatedClass(Promocao.class);
		configuration.addAnnotatedClass(ProdutoDetalhePromocao.class);
		configuration.addAnnotatedClass(DiaSemana.class);
		configuration.addAnnotatedClass(Cliente.class);
		configuration.addAnnotatedClass(ProdutoDetalhe.class);

		if(factory == null){
			System.out.println("Contruindo factory");
			factory = configuration.buildSessionFactory(); 
		} 
	}
	
	public void atualizaConexao() {
		properties = returnArquivoConfiguracao(caminhoMysql(), "config_bd.properties");

		configuration = new AnnotationConfiguration(); 
		configuration.setProperty("hibernate.current_session_context_class", "thread");
		configuration.setProperty("hibernate.dialect", properties.getProperty("dialect"));
		configuration.setProperty("hibernate.connection.driver_class", properties.getProperty("driver_class"));
		configuration.setProperty("hibernate.connection.url", properties.getProperty("url"));
		configuration.setProperty("hibernate.connection.username", properties.getProperty("username"));
		configuration.setProperty("hibernate.connection.password", properties.getProperty("password"));
		configuration.setProperty("hibernate.show_sql", properties.getProperty("show_sql"));
		configuration.setProperty("hibernate.hbm2ddl.auto", properties.getProperty("hbm2ddl"));

		configuration.addAnnotatedClass(Usuario.class);
		configuration.addAnnotatedClass(Grupo.class);
		configuration.addAnnotatedClass(Permissao.class);
		configuration.addAnnotatedClass(Cardapio.class);
		configuration.addAnnotatedClass(Cidade.class);
		configuration.addAnnotatedClass(Empresa.class);
		configuration.addAnnotatedClass(Estado.class);
		configuration.addAnnotatedClass(Produto.class);
		configuration.addAnnotatedClass(Acrescimo.class);
		configuration.addAnnotatedClass(TpAlimento.class);
		configuration.addAnnotatedClass(Vigencia.class);
		configuration.addAnnotatedClass(VigenciaAcrescimo.class);
		configuration.addAnnotatedClass(Promocao.class);
		configuration.addAnnotatedClass(ProdutoDetalhePromocao.class);
		configuration.addAnnotatedClass(DiaSemana.class);
		configuration.addAnnotatedClass(Cliente.class);
		configuration.addAnnotatedClass(ProdutoDetalhe.class);

		System.out.println("Atualizando factory");
		factory = configuration.buildSessionFactory(); 
	}

	public Session getSession() {
		Session session = factory.openSession();
		return session;
	}

	public Session getSessionTeste() {
		properties = returnArquivoConfiguracao(caminhoMysql(), "config_bd.properties");
		System.out.println("SENHA: "+properties.getProperty("password"));

		configuration = new AnnotationConfiguration(); 
		configuration.setProperty("hibernate.current_session_context_class", "thread");
		configuration.setProperty("hibernate.dialect", properties.getProperty("dialect"));
		configuration.setProperty("hibernate.connection.driver_class", properties.getProperty("driver_class"));
		configuration.setProperty("hibernate.connection.url", properties.getProperty("url"));
		configuration.setProperty("hibernate.connection.username", properties.getProperty("username"));
		configuration.setProperty("hibernate.connection.password", properties.getProperty("password"));
		configuration.setProperty("hibernate.show_sql", properties.getProperty("show_sql"));
		configuration.setProperty("hibernate.hbm2ddl.auto", properties.getProperty("hbm2ddl"));

		configuration.addAnnotatedClass(Usuario.class);
		configuration.addAnnotatedClass(Grupo.class);
		configuration.addAnnotatedClass(Permissao.class);
		configuration.addAnnotatedClass(Cardapio.class);
		configuration.addAnnotatedClass(Cidade.class);
		configuration.addAnnotatedClass(Empresa.class);
		configuration.addAnnotatedClass(Estado.class);
		configuration.addAnnotatedClass(Produto.class);
		configuration.addAnnotatedClass(Acrescimo.class);
		configuration.addAnnotatedClass(TpAlimento.class);
		configuration.addAnnotatedClass(Vigencia.class);
		configuration.addAnnotatedClass(VigenciaAcrescimo.class);
		configuration.addAnnotatedClass(Promocao.class);
		configuration.addAnnotatedClass(ProdutoDetalhePromocao.class);
		configuration.addAnnotatedClass(DiaSemana.class);
		configuration.addAnnotatedClass(Cliente.class);
		configuration.addAnnotatedClass(ProdutoDetalhe.class);

		System.out.println("Contruindo factoryTeste");
		factoryTeste = configuration.buildSessionFactory();

		Session session = factoryTeste.openSession();
		return session;
	}

	private Properties returnArquivoConfiguracao(String caminhoArquivo, String nomeArquivo) {
		Parametros p = new Parametros();
		try {
			properties = p.read_access(caminhoArquivo, nomeArquivo);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return properties;
	}

	public String caminhoMysql(){
		if(System.getProperty("os.name").equals("Linux") == true){
			return "/tcf/";
		}else{
			return "C:\\tcf\\";
		}
	}
}